package com.wh.wisdomsite.xbox.server.controller.wo;

import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.pojo.wo.groupInfo.GroupDetail;
import com.wh.wisdomsite.xbox.pojo.wo.groupInfo.GroupInfoIn;
import com.wh.wisdomsite.xbox.pojo.wo.groupInfo.GroupInfoOut;
import com.wh.wisdomsite.xbox.pojo.wo.groupInfo.GroupListOut;
import com.wh.wisdomsite.xbox.pojo.wo.workerInfo.WorkerInfoOut;
import com.wh.wisdomsite.xbox.pr.entity.Project;
import com.wh.wisdomsite.xbox.pr.service.IProjectService;
import com.wh.wisdomsite.xbox.utils.Const;
import com.wh.wisdomsite.xbox.utils.IDCardUtils;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.utils.server.connToserver_HttpClientClose;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 劳务班组信息controller
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.server.controller.wo
 * @date 2018年09月20日 9:32
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/groupInfo")
public class GroupInfoController {
    private static final Log logger = LogFactory.getLog(GroupInfoController.class);

    @Autowired
    private IProjectService iProjectService;

    /**
     * 跳转到班组列表信息
     * @param request
     * @param prId
     * @return
     */
    @RequiresPermissions("rules:wo_groupInfo_page")
    @RequestMapping(value = "/index.do", method = RequestMethod.GET)
    public String index(HttpServletRequest request,String prId) {
        request.setAttribute("prId",prId);
        return "wo/groupInfo/groupInfoList";
    }

    /**
     * 获取班组列表
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:wo_groupInfo_find")
    @RequestMapping(value = "/groupInfoList.do")
    public void groupInfoList(HttpServletResponse response, GroupInfoIn in) {
        logger.info("加载劳务班组列表-->>开始");
        try {
            if (in.getPage() == 0) in.setPage(1);
            if (in.getRows() == 0) in.setRows(10);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            int total = 0;
            //获取项目名称
            String projectName = "";
            Project project = iProjectService.selectById(in.getProjectId());
            if (null != project) {
                projectName = project.getPrName();
            }
            //接口查询条件
            Map<String, String> map = new HashMap<String, String>();
            map.put("pageNumber", String.valueOf(in.getPage()));
            map.put("pageSize", String.valueOf(in.getRows()));
            map.put("projectIds", in.getProjectId());
            if(StringUtils.isNotEmpty(in.getGroupName())){
                map.put("teamName", in.getGroupName());
            }
            //接口查询结果
            String result = connToserver_HttpClientClose.sendPostRequest(Const.WORKER_GROUPPAGEURL, map);
            if(result.contains("projectId")){
                result = result.replaceAll("Work","work").replaceAll("Group","group").replaceAll("null","\"\"");
            }
            //对象中存在集合的需要通过map注明集合的类型，否则会报错
            Map classMap = new HashMap();
            classMap.put("groupObj",GroupInfoOut.class);
            net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(result);
            GroupListOut groups = (GroupListOut)net.sf.json.JSONObject.toBean(jsonObject, GroupListOut.class,classMap);
            total = groups.getGroupsObj().getCount();
            //班组数据集合
            List<GroupInfoOut> groupList = groups.getGroupsObj().getGroupObj();
            for(GroupInfoOut group : groupList){
                JSONObject jsonObj = new JSONObject(group);
                jsonObj.put("projectName",projectName);
                array.put(jsonObj);
            }
            json.put("total", total);
            json.put("rows", array);
            ResManager.getJsonOut(response).print(json);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取劳务班组列表-->Exception " + e.getMessage());
        }
    }

    /**
     * 获取班组详细信息
     * @param request
     * @param groupInfoOut
     */
    @RequestMapping(value = "/groupInfoDesc.do")
    public String workerInfoDesc(HttpServletRequest request, GroupInfoOut groupInfoOut){
        logger.info("加载班组信息-->>开始");
        request.setAttribute("groupInfo",groupInfoOut);
        logger.info("加载班组信息-->>结束");
        return "/wo/groupInfo/groupInfoDesc";
    }


    /**
     * 跳转班组成员信息
     * @param request
     * @param groupInfoOut
     * @return
     */
    @RequiresPermissions("rules:wo_groupInfo_view")
    @RequestMapping(value = "/toGroupMemberInfo.do")
    public String toGroupMemberInfo(HttpServletRequest request, GroupInfoOut groupInfoOut){
        logger.info("跳转班组成员信息-->>开始");
        //农名工人员信息集合
        List<WorkerInfoOut> workerInfoList = new ArrayList<WorkerInfoOut>();
        try{
            String groupId = groupInfoOut.getGroupId();
            String projectId = groupInfoOut.getProjectId();
            if(StringUtils.isNotEmpty(groupId) && StringUtils.isNotEmpty(projectId)){
                //接口查询条件
                Map<String, String> map = new HashMap<String, String>();
                map.put("groupId", groupId);
                map.put("projectId", projectId);
                //接口查询结果
                String result = connToserver_HttpClientClose.sendPostRequest(Const.WORKER_PERSONBYGROUPURL, map);
                if(result.contains("idCard")){
                    result = result.replaceAll("Work","work").replaceAll("Group","group").replaceAll("null","\"\"");
                }
                //对象中存在集合的需要通过map注明集合的类型，否则会报错
                Map classMap = new HashMap();
                classMap.put("resolve",WorkerInfoOut.class);
                net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(result);
                GroupDetail workerDetail = (GroupDetail)net.sf.json.JSONObject.toBean(jsonObject, GroupDetail.class,classMap);
                workerInfoList = workerDetail.getResolve();
                for(WorkerInfoOut workerInfo : workerInfoList){
                    String idCard = workerInfo.getIdCard();
                    //通过身份证号获取人的年龄
                    Integer age = null;
                    if(idCard.length() > 15){
                        Map<String, Object> mapCard = IDCardUtils.getCarInfo(idCard);
                        age = (Integer) mapCard.get("age");
                    }else{
                        Map<String, Object> mapCard = IDCardUtils.getCarInfo15W(idCard);
                        age = (Integer) mapCard.get("age");
                    }
                    workerInfo.setAge(age);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("跳转班组成员信息->>Exception " + e.getMessage());
        }
        request.setAttribute("groupInfoOut",groupInfoOut);
        request.setAttribute("workerInfoList",workerInfoList);
        logger.info("跳转班组成员信息-->>开始");
        return "/wo/groupInfo/groupMemberInfo";
    }

}
